package com.bb.flow.parser.impl.parsejson.parsenode.nodeimp;

import com.alibaba.fastjson2.JSONObject;
import com.bb.flow.api.exception.ParseException;
import com.bb.flow.config.AbstractConfigNode;
import com.bb.flow.config.ClassInfo;
import com.bb.flow.config.node.FunctionConfigNode;

/**
 * @author bigbeard
 * 解析功能类型的节点
 */
public class ParseFunctionNode extends AbstractNodeImp {

    @Override
    public AbstractConfigNode parse(JSONObject jsonObject, String flowTemplateName, String parentNodeKey) throws ParseException {
        FunctionConfigNode functionConfigNode = new FunctionConfigNode();

        boolean b = parseNodeBaseInfo.parseNodeBaseInfo(functionConfigNode, jsonObject, flowTemplateName);
        if (!b) {
            return null;
        }
        String nodeKey = getNodeKey(parentNodeKey, functionConfigNode);

        JSONObject classInfoJson = jsonObject.getJSONObject("ClassInfo");
        if (null == classInfoJson) {
            String s = String.format("流程模版[%s]节点,条件解析节点[%s]必须配置[ClassInfo]属性,否则无法解析条件",
                    flowTemplateName, nodeKey);
            throw new ParseException(s);
        }

        ClassInfo classInfo = parseClassInfo.parseClassInfo(classInfoJson, nodeKey, flowTemplateName);
        if (null != classInfo) {
            functionConfigNode.setClassInfo(classInfo);
        } else {
            String s = String.format("流程模版[%s]节点,条件解析节点[%s]解析[ClassInfo]发生错误",
                    flowTemplateName, nodeKey);
            throw new ParseException(s);
        }
        return functionConfigNode;
    }

}